//
//  CJBrowseUIImageView.m
//  ImagesBrowse
//
//  Created by hetao on 14-7-30.
//  Copyright (c) 2014年 hetao. All rights reserved.
//

#import "CJBrowseUIImageView.h"

@implementation CJBrowseUIImageView
int indexs;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imagelook:)];
        self.userInteractionEnabled=YES;
        [self addGestureRecognizer:tap];
        
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self=[super initWithCoder:aDecoder];
    if(self)
    {
        tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imagelook:)];
        self.userInteractionEnabled=YES;
        [self addGestureRecognizer:tap];
    }
    return self;
}

///初始化
- (void)initImageBackgroundView
{
    if(!self.imageBackgroundView)
    {
       self.imageBackgroundView= [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HHEIGHT)];
         [self initImageScrollView];
        [self.imageBackgroundView addSubview:self.imageScrollView];
         [[[UIApplication sharedApplication] keyWindow] addSubview:self.imageBackgroundView];
        [self.imageBackgroundView setBackgroundColor:[UIColor blackColor]];
        self.imageBackgroundView.hidden=YES;
    }
 
}

///初始化imageScrollView
- (void)initImageScrollView
{
    
    if(!self.imageScrollView)
    {
        self.imageScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 20, WIDTH, HHEIGHT-20)];
        self.imageScrollView.pagingEnabled=YES;
        self.imageScrollView.showsHorizontalScrollIndicator=NO;
        self.imageScrollView.showsVerticalScrollIndicator=NO;
        [self.imageScrollView setDelegate:self];
    }

}

///向imageScrollView添加图片数据
- (void)addImageScrollViewData
{
    indexs=0;
    
    if(imagedata.count>1)
    {
        [self.imageScrollView setContentSize:CGSizeMake(self.imageScrollView.frame.size.width*3, 0)];
        [self.imageScrollView setContentOffset:CGPointMake(self.imageScrollView.frame.size.width, 0) animated:NO];
    }else{
        [self.imageScrollView setContentSize:CGSizeMake(0, 0)];
    }
    
    [self initimagecellfile];
    int i=0;
    for (UIImage *image in imagedata)
    {
        
        if([self.image isEqual:image])
        {
            indexs=i;
            [self imageScrollViewRow:i];
            return;
        }
        i++;
    }
    [self imageScrollViewRow:0];
    
}

/// 实现重用机制
- (void)imageScrollViewRow:(int)row
{
    
    UIImageView *imageview1=[imagecellfile objectAtIndex:0];
    UIImageView *imageview2=[imagecellfile objectAtIndex:1];
    UIImageView *imageview3=[imagecellfile objectAtIndex:2];
    if(imagedata.count==1)//当只显示1张图片的情况
    {
        imageview1.image=imagedata[0];
        return;
    }
    if(imagedata.count==2)//当只显示两张图片的情况
    {
        if(row==0)//当显示第一张图片时
        {
            imageview1.image=imagedata[imagedata.count-1];
            imageview3.image=imagedata[imagedata.count-1];
        }else if (row==imagedata.count-1)//当显示最后张图片时
        {
            imageview1.image=imagedata[0];
            imageview3.image=imagedata[0];
        }else
        imageview2.image=imagedata[row];
        
    }else//当只显示三张图片以上的情况

    {
        if(row==0)//当显示第一张图片时
        {
            imageview1.image=imagedata[imagedata.count-1];
            imageview3.image=imagedata[row+1];
        }else if (row==imagedata.count-1)//当显示最后张图片时
        {
            imageview1.image=imagedata[row-1];
            imageview3.image=imagedata[0];
        }else
        {
            imageview1.image=imagedata[row-1];
            imageview3.image=imagedata[row+1];
        }
        imageview2.image=imagedata[row];
    }
   
    
    
}

///初始化图片重用队列
- (void)initimagecellfile
{
    //图片重用队列是否存在
    if(!imagecellfile)
    {
        imagecellfile=[[NSMutableArray alloc]init];
        
        //重用队列中只存在3个UIImageView
        for(int index=0;index<3;index++)
        {
            UIImageView *imageview=[[UIImageView alloc]initWithFrame:CGRectMake(index*self.imageScrollView.frame.size.width, 0, self.imageScrollView.frame.size.width, self.imageScrollView.frame.size.height)];
            UITapGestureRecognizer *imagetap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageTap:)];
            [imageview addGestureRecognizer:imagetap];
            [imageview setUserInteractionEnabled:YES];
            [imagecellfile addObject:imageview];
            [self.imageScrollView addSubview:imageview];
        }
    }

}

- (void)imageTap:(UITapGestureRecognizer *)tap
{
    [self hiddenFrame];
}
///查看图片显示准备
- (void)showImages:(NSArray *)arr
{
    if(arr)
    {
        imagedata=arr;
    }
    
}


///显示查看
- (void)show
{
    [self initImageBackgroundView];
  
     //self.imageBackgroundView.hidden=NO;
    [self showFrame];
      [self addImageScrollViewData];
}

///显示动画
- (void)showFrame
{
    //坐标系转换，获取当前视图在WINDOW中坐标比
    CGRect selfRect=self.frame;
   
    CGRect windowRect=[self convertRect:selfRect toView:[UIApplication sharedApplication].keyWindow];
    
    //不知道为什么，在坐标转换到window中，坐标系会是原来的两倍
    windowRect.origin.x=windowRect.origin.x/2;
    windowRect.origin.y=windowRect.origin.y/2;
    //[self.imageScrollView setContentOffset:CGPointMake(<#CGFloat x#>, <#CGFloat y#>)]
      self.imageBackgroundView.hidden=NO;
    [self.imageBackgroundView setFrame:windowRect];
     [self.imageScrollView setFrame:CGRectMake(0, 0, windowRect.size.width, windowRect.size.height)];
    [UIView animateWithDuration:.3 animations:^{
        [self.imageBackgroundView setFrame:CGRectMake(0, 0, WIDTH, HHEIGHT)];
        [self.imageScrollView setFrame:CGRectMake(0, 20, WIDTH, HHEIGHT-20)];
    }];
}

///隐藏动画
- (void)hiddenFrame
{
    //坐标系转换，获取当前视图在WINDOW中坐标比
    CGRect selfRect=self.frame;
    
    CGRect windowRect=[self convertRect:selfRect toView:[UIApplication sharedApplication].keyWindow];
    
    //不知道为什么，在坐标转换到window中，坐标系会是原来的两倍
    windowRect.origin.x=windowRect.origin.x/2;
    windowRect.origin.y=windowRect.origin.y/2;
    
    [UIView animateWithDuration:0.3 animations:^{
        [self.imageBackgroundView setFrame:windowRect];
           [self.imageScrollView setFrame:CGRectMake(0, 20, windowRect.size.width, windowRect.size.height)];
    } completion:^(BOOL finished) {
        self.imageBackgroundView.hidden=YES;
    }];
}
-(void)imagelook:(UITapGestureRecognizer *)tap
{
    if(imagedata)
    {
        [self show];
    }
}

#pragma mark-------scrollview代理-------
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
   // NSLog(@"%f",scrollView.contentOffset.x);
    CGFloat x=scrollView.contentOffset.x/self.imageScrollView.frame.size.width;
    if(x==0)//往左滑动
    {
        indexs--;
    }
    if(x==1)//还是停留在当前
    {
        
    }
    if(x==2)//往右滑动

    {
        indexs++;
    }
    
    if(indexs==-1)
    {
        indexs=(int)imagedata.count-1;
    }

    if(indexs>(imagedata.count-1))
    {
        indexs=0;
    }
    
    NSLog(@"%d",indexs);
    [self imageScrollViewRow:indexs];
    [scrollView setContentOffset:CGPointMake(self.imageScrollView.frame.size.width, 0)];
    

}
@end
